<?php

namespace App\Api;

use App\Http\Controllers\Controller;
use Auth;
use Illuminate\Http\Request;
use PermissionService;
use Spatie\Permission\Models\Role;

class PermissionController extends Controller
{

    public function init()
    {

        PermissionService::generate();//生成权限表
        return $this->message('权限初始化成功');
    }

    //获取所有权限
    public function getAllPermissions()
    {
        $results = PermissionService::getPermissions();
        return $results;
    }


    public function syncPermissions(Request $request, Role $role)
    {
        $role->syncPermissions($request->input('permissions'));
        return $this->message('权限修改成功');
    }


    public function rolehasPermissions(Role $role)
    {
        return $role->permissions()->get();
    }

    public function currUserPermission()
    {
        $user = Auth::user();
        return $user->getPermissionsViaRoles()->pluck('name');
    }
}
